
*------------------------------------------------------------------------------*
* COUNCIL OF MINISTERS
*
* Additional specs: none
*
* Version:	2016-10-28
* Changes:	2016-??-??: First release
*------------------------------------------------------------------------------*


use tmp_eu, clear


* ----------------------
* Reduce to country file (EU members only)
by p601 g105 g101, sort: drop if _n!=_N
drop if p601==.

gen _____COUNCIL_OF_MINISTERS = .
save tmp_councilofmin, replace



* ----------------------
* Standard Positions


* Position of the presidency
gen zs5 = GOV_POS if p614==1
by p601 g105, sort: egen couofmin_pres = max(zs5)
drop zs5


* Mean position, min and max, range
by p601 g105, sort: egen couofmin_mean_uw	=   mean(GOV_POS)
by p601 g105, sort: egen couofmin_median_uw = median(GOV_POS)
by p601 g105, sort: egen couofmin_min		=    min(GOV_POS)
by p601 g105, sort: egen couofmin_max	 	=    max(GOV_POS)
gen couofmin_range = couofmin_max - couofmin_min
gen couofmin_unanimity = couofmin_min + (couofmin_max - couofmin_min)/2

label var couofmin_mean_uw 		"Council of Ministers: Unweighted mean position"
label var couofmin_median_uw	"Council of Ministers: Unweighted median position"
label var couofmin_min 			"Council of Ministers: Minimum position"
label var couofmin_max 			"Council of Ministers: Maximum position"
label var couofmin_unanimity	"Council of Ministers: Position under unanimity rule"
label var couofmin_range 		"Council of Ministers: Range inbetween European Council"
label var couofmin_pres			"Council of Ministers: Position of the Presidency"
save, replace


* Mean and median position weighted by votes
collapse (median) couofmin_median = GOV_POS (mean) couofmin_mean = GOV_POS [fweight = p611], by(p601 g105)
save zs3, replace
use tmp_councilofmin, clear
merge p601 g105 using zs3, sort uniqusing
drop _merge

label var couofmin_mean 	"Council of Ministers: Mean position weighted by votes"
label var couofmin_median	"Council of Ministers: Median position weighted by votes"



* ----------------------
* Position under QMV with fixed presidency (qmv1 or qmv1_uw)

if	"$council1" == "qmv1" | "$council1" == "qmv1_uw" | "$council1" == "qmv2" | "$council1" == "qmv2_uw" |	///
	"$council2" == "qmv1" | "$council2" == "qmv1_uw" | "$council2" == "qmv2" | "$council2" == "qmv2_uw" |	///
	"$council3" == "qmv1" | "$council3" == "qmv1_uw" | "$council3" == "qmv2" | "$council3" == "qmv2_uw" |	///
	"$council4" == "qmv1" | "$council4" == "qmv1_uw" | "$council4" == "qmv2" | "$council4" == "qmv2_uw" |	///
	"$council5" == "qmv1" | "$council5" == "qmv1_uw" | "$council5" == "qmv2" | "$council5" == "qmv2_uw"		///
{

	gen range = abs(couofmin_pres - GOV_POS)	// range of each gov to presidency
	sort p601 g105 range p611 

	gen votes = p611 if p614==1	// count votes
	replace votes = votes[_n-1] + p611 if p601[_n-1]==p601 & g105[_n-1]==g105

	gen participant = 1 if p614==1	// mark participants
	replace participant = 1 if votes < p612
	replace participant = 1 if participant[_n-1]==1 & votes[_n-1] < p612

	gen zs6 = GOV_POS if participant == 1	// unweighted mean position
	by p601 g105, sort: egen couofmin_qmv1_uw = mean(zs6)

	gen zsnew = p611 if participant == 1	// weighted mean position
	by p601 g105, sort: egen newtot = sum(zsnew)
	gen zs8 = GOV_POS * (zsnew / newtot)
	by p601 g105, sort: egen couofmin_qmv1 = sum(zs8)

	drop range votes participant newtot zs*
	
	lab var couofmin_qmv1_uw	"Council of Ministers: Pos. under QMV - unweighted mean of MCWC"
	lab var couofmin_qmv1		"Council of Ministers: Pos. under QMV - mean of MCWC weighted by votes"

}


* ----------------------
* Position under QMV with rotating presidency since 2010 (qmv2 or qmv2_uw)

if	"$council1" == "qmv2" | "$council1" == "qmv2_uw" |	///
	"$council2" == "qmv2" | "$council2" == "qmv2_uw" |	///
	"$council3" == "qmv2" | "$council3" == "qmv2_uw" |	///
	"$council4" == "qmv2" | "$council4" == "qmv2_uw" |	///
	"$council5" == "qmv2" | "$council5" == "qmv2_uw"	///
{

	sort p601 g105 g101
	local rotprespowerl "$rotprespower"	// "translate" global to local, which can be tokenized
	*di "`rotprespowerl'"
	gen pstrio = "`rotprespowerl'" if g105 >= tq(2010q1)
	lab var pstrio "Power scores for presidency trio"
	tokenize `rotprespowerl', parse(":")	// tokenize power scores for rotating presidency
	*di "`1'"
	*di "`3'"
	*di "`5'"

	gen zs_rot_pres = .
	replace zs_rot_pres = `1' if p615 == 3	// current
	replace zs_rot_pres = `3' if p615 == 2	// successor
	replace zs_rot_pres = `5' if p615 == 1	// upcoming
	by p601 g105, sort: egen zs_rot_total = sum(zs_rot_pres)
	replace zs_rot_total = . if g105 < tq(2010q1)

	gen zs = GOV_POS * (zs_rot_pres / zs_rot_total)	// weigh positions
	by p601 g105, sort: egen couofmin_pres2 = sum(zs)
	
	gen range = abs(couofmin_pres2 - GOV_POS)	// range of each gov to presidency
	replace range = 0 if zs_rot_pres != .
	gsort p601 g105 range -zs_rot_pres p611

	gen votes = p611 if p615 == 3	// count votes
	replace votes = votes[_n-1] + p611 if p601[_n-1]==p601 & g105[_n-1]==g105

	gen participant = 1 if zs_rot_pres != .	// mark participants
	replace participant = 1 if votes < p612 & participant == .
	replace participant = 1 if participant[_n-1]==1 & votes[_n-1] < p612

	gen zs6 = GOV_POS if participant == 1	// unweighted mean position
	by p601 g105, sort: egen couofmin_qmv2_uw = mean(zs6)

	gen zsnew = p611 if participant == 1	// weighted mean position
	by p601 g105, sort: egen newtot = sum(zsnew)
	gen zs8 = GOV_POS * (zsnew / newtot)
	by p601 g105, sort: egen couofmin_qmv2 = sum(zs8)
	
	drop range votes participant newtot zs*

	replace couofmin_pres2	 = couofmin_pres 	if g105 < tq(2010q1)	// fill in qmv1 pre-2010 if qmv2 is erroneously specified pre-Lisbon 
	replace couofmin_qmv2_uw = couofmin_qmv1_uw if g105 < tq(2010q1) 
	replace couofmin_qmv2 	 = couofmin_qmv1 	if g105 < tq(2010q1) 

	lab var	couofmin_pres2		"Council of Ministers: Position of Rotating Presidency"
	lab var couofmin_qmv2_uw	"Council of Ministers: Pos. under QMV - unweighted mean of MCWC"
	lab var couofmin_qmv2		"Council of Ministers: Pos. under QMV - mean of MCWC weighted by votes"
	
}


* ----------------------
* Model Choice

gen COUNCIL_POS = .
gen councilofministers = ""

lab var COUNCIL_POS		 	"EU: Position of Council of Ministers"
lab var councilofministers	"Your model choice for the Council of Ministers"


// "Luxembourg compromise", consultation ( -1987q2)
	if 	"$council1" == "mean" {
		replace COUNCIL_POS = couofmin_mean			if g105<=tq(1987q2)
		replace councilofministers = "$council1"	if g105<=tq(1987q2)
	}
	else if "$council1" == "mean_uw" {
		replace COUNCIL_POS = couofmin_mean_uw		if g105<=tq(1987q2)
		replace councilofministers = "$council1"	if g105<=tq(1987q2)
	}
	else if "$council1" == "median" {
		replace COUNCIL_POS = couofmin_median		if g105<=tq(1987q2)
		replace councilofministers = "$council1"	if g105<=tq(1987q2)
	}
	else if "$council1" == "median_uw" {
		replace COUNCIL_POS = couofmin_median_uw	if g105<=tq(1987q2)
		replace councilofministers = "$council1"	if g105<=tq(1987q2)
	}
	else if "$council1" == "unanimity" {
		replace COUNCIL_POS = couofmin_unanimity	if g105<=tq(1987q2)
		replace councilofministers = "$council1"	if g105<=tq(1987q2)
	}
	else if "$council1" == "qmv1" {
		replace COUNCIL_POS = couofmin_qmv1			if g105<=tq(1987q2)
		replace councilofministers = "$council1"	if g105<=tq(1987q2)
	}
	else if "$council1" == "qmv1_uw" {
		replace COUNCIL_POS = couofmin_qmv1_uw		if g105<=tq(1987q2)
		replace councilofministers = "$council1"	if g105<=tq(1987q2)
	}
	else if "$council1" == "qmv2" {
		replace COUNCIL_POS = couofmin_qmv2			if g105<=tq(1987q2)
		replace councilofministers = "$council1"	if g105<=tq(1987q2)
	}
	else if "$council1" == "qmv2_uw" {
		replace COUNCIL_POS = couofmin_qmv2_uw		if g105<=tq(1987q2)
		replace councilofministers = "$council1"	if g105<=tq(1987q2)
	}


// Single European Act, cooperation (1987q3-1993q3)
	if 	"$council2" == "mean" {
		replace COUNCIL_POS = couofmin_mean			if g105 >=tq(1987q3) & g105<=tq(1993q3)
		replace councilofministers = "$council2"	if g105 >=tq(1987q3) & g105<=tq(1993q3)
	}
	else if "$council2" == "mean_uw" {
		replace COUNCIL_POS = couofmin_mean_uw		if g105 >=tq(1987q3) & g105<=tq(1993q3)
		replace councilofministers = "$council2"	if g105 >=tq(1987q3) & g105<=tq(1993q3)
	}
	else if "$council2" == "median" {
		replace COUNCIL_POS = couofmin_median		if g105 >=tq(1987q3) & g105<=tq(1993q3)
		replace councilofministers = "$council2"	if g105 >=tq(1987q3) & g105<=tq(1993q3)
	}
	else if "$council2" == "median_uw" {
		replace COUNCIL_POS = couofmin_median_uw	if g105 >=tq(1987q3) & g105<=tq(1993q3)
		replace councilofministers = "$council2"	if g105 >=tq(1987q3) & g105<=tq(1993q3)
	}
	else if "$council2" == "unanimity" {
		replace COUNCIL_POS = couofmin_unanimity	if g105 >=tq(1987q3) & g105<=tq(1993q3)
		replace councilofministers = "$council2"	if g105 >=tq(1987q3) & g105<=tq(1993q3)
	}
	else if "$council2" == "qmv1" {
		replace COUNCIL_POS = couofmin_qmv1			if g105 >=tq(1987q3) & g105<=tq(1993q3)
		replace councilofministers = "$council2"	if g105 >=tq(1987q3) & g105<=tq(1993q3)
	}
	else if "$council2" == "qmv1_uw" {
		replace COUNCIL_POS = couofmin_qmv1_uw		if g105 >=tq(1987q3) & g105<=tq(1993q3)
		replace councilofministers = "$council2"	if g105 >=tq(1987q3) & g105<=tq(1993q3)
	}
	else if "$council2" == "qmv2" {
		replace COUNCIL_POS = couofmin_qmv2			if g105 >=tq(1987q3) & g105<=tq(1993q3)
		replace councilofministers = "$council2"	if g105 >=tq(1987q3) & g105<=tq(1993q3)
	}
	else if "$council2" == "qmv2_uw" {
		replace COUNCIL_POS = couofmin_qmv2_uw		if g105 >=tq(1987q3) & g105<=tq(1993q3)
		replace councilofministers = "$council2"	if g105 >=tq(1987q3) & g105<=tq(1993q3)
	}


// Maastricht Treaty, co-decision I (1993q4 - 1999q1)
	if 	"$council3" == "mean" {
		replace COUNCIL_POS = couofmin_mean			if g105 >=tq(1993q4) & g105<=tq(1999q1)
		replace councilofministers = "$council3"	if g105 >=tq(1993q4) & g105<=tq(1999q1)
	}
	else if "$council3" == "mean_uw" {
		replace COUNCIL_POS = couofmin_mean_uw		if g105 >=tq(1993q4) & g105<=tq(1999q1)
		replace councilofministers = "$council3"	if g105 >=tq(1993q4) & g105<=tq(1999q1)
	}
	else if "$council3" == "median" {
		replace COUNCIL_POS = couofmin_median		if g105 >=tq(1993q4) & g105<=tq(1999q1)
		replace councilofministers = "$council3"	if g105 >=tq(1993q4) & g105<=tq(1999q1)
	}
	else if "$council3" == "median_uw" {
		replace COUNCIL_POS = couofmin_median_uw	if g105 >=tq(1993q4) & g105<=tq(1999q1)
		replace councilofministers = "$council3"	if g105 >=tq(1993q4) & g105<=tq(1999q1)
	}
	else if "$council3" == "unanimity" {
		replace COUNCIL_POS = couofmin_unanimity	if g105 >=tq(1993q4) & g105<=tq(1999q1)
		replace councilofministers = "$council3"	if g105 >=tq(1993q4) & g105<=tq(1999q1)
	}
	else if "$council3" == "qmv1" {
		replace COUNCIL_POS = couofmin_qmv1			if g105 >=tq(1993q4) & g105<=tq(1999q1)
		replace councilofministers = "$council3"	if g105 >=tq(1993q4) & g105<=tq(1999q1)
	}
	else if "$council3" == "qmv1_uw" {
		replace COUNCIL_POS = couofmin_qmv1_uw		if g105 >=tq(1993q4) & g105<=tq(1999q1)
		replace councilofministers = "$council3"	if g105 >=tq(1993q4) & g105<=tq(1999q1)
	}
	else if "$council3" == "qmv2" {
		replace COUNCIL_POS = couofmin_qmv2			if g105 >=tq(1993q4) & g105<=tq(1999q1)
		replace councilofministers = "$council3"	if g105 >=tq(1993q4) & g105<=tq(1999q1)
	}
	else if "$council3" == "qmv2_uw" {
		replace COUNCIL_POS = couofmin_qmv2_uw		if g105 >=tq(1993q4) & g105<=tq(1999q1)
		replace councilofministers = "$council3"	if g105 >=tq(1993q4) & g105<=tq(1999q1)
	}


// Amsterdam Treaty, co-decision II (1999q2-2009q4)
	if 	"$council4" == "mean" {
		replace COUNCIL_POS = couofmin_mean			if g105 >=tq(1999q2) & g105<=tq(2009q4)
		replace councilofministers = "$council4"	if g105 >=tq(1999q2) & g105<=tq(2009q4)
	}
	else if "$council4" == "mean_uw" {
		replace COUNCIL_POS = couofmin_mean_uw		if g105 >=tq(1999q2) & g105<=tq(2009q4)
		replace councilofministers = "$council4"	if g105 >=tq(1999q2) & g105<=tq(2009q4)
	}
	else if "$council4" == "median" {
		replace COUNCIL_POS = couofmin_median		if g105 >=tq(1999q2) & g105<=tq(2009q4)
		replace councilofministers = "$council4"	if g105 >=tq(1999q2) & g105<=tq(2009q4)
	}
	else if "$council4" == "median_uw" {
		replace COUNCIL_POS = couofmin_median_uw	if g105 >=tq(1999q2) & g105<=tq(2009q4)
		replace councilofministers = "$council4"	if g105 >=tq(1999q2) & g105<=tq(2009q4)
	}
	else if "$council4" == "unanimity" {
		replace COUNCIL_POS = couofmin_unanimity	if g105 >=tq(1999q2) & g105<=tq(2009q4)
		replace councilofministers = "$council4"	if g105 >=tq(1999q2) & g105<=tq(2009q4)
	}
	else if "$council4" == "qmv1" {
		replace COUNCIL_POS = couofmin_qmv1			if g105 >=tq(1999q2) & g105<=tq(2009q4)
		replace councilofministers = "$council4"	if g105 >=tq(1999q2) & g105<=tq(2009q4)
	}
	else if "$council4" == "qmv1_uw" {
		replace COUNCIL_POS = couofmin_qmv1_uw		if g105 >=tq(1999q2) & g105<=tq(2009q4)
		replace councilofministers = "$council4"	if g105 >=tq(1999q2) & g105<=tq(2009q4)
	}
	else if "$council4" == "qmv2" {
		replace COUNCIL_POS = couofmin_qmv2			if g105 >=tq(1999q2) & g105<=tq(2009q4)
		replace councilofministers = "$council4"	if g105 >=tq(1999q2) & g105<=tq(2009q4)
	}
	else if "$council4" == "qmv2_uw" {
		replace COUNCIL_POS = couofmin_qmv2_uw		if g105 >=tq(1999q2) & g105<=tq(2009q4)
		replace councilofministers = "$council4"	if g105 >=tq(1999q2) & g105<=tq(2009q4)
	}


// Lisbon Treaty, co-decision II with rotating presidency (2010q1- )
	if 	"$council5" == "mean" {
		replace COUNCIL_POS = couofmin_mean			if g105 >=tq(2010q1)
		replace councilofministers = "$council5"	if g105 >=tq(2010q1)
	}
	else if "$council5" == "mean_uw" {
		replace COUNCIL_POS = couofmin_mean_uw		if g105 >=tq(2010q1)
		replace councilofministers = "$council5"	if g105 >=tq(2010q1)
	}
	else if "$council5" == "median" {
		replace COUNCIL_POS = couofmin_median		if g105 >=tq(2010q1)
		replace councilofministers = "$council5"	if g105 >=tq(2010q1)
	}
	else if "$council5" == "median_uw" {
		replace COUNCIL_POS = couofmin_median_uw	if g105 >=tq(2010q1)
		replace councilofministers = "$council5"	if g105 >=tq(2010q1)
	}
	else if "$council5" == "unanimity" {
		replace COUNCIL_POS = couofmin_unanimity	if g105 >=tq(2010q1)
		replace councilofministers = "$council5"	if g105 >=tq(2010q1)
	}
	else if "$council5" == "qmv1" {
		replace COUNCIL_POS = couofmin_qmv1			if g105 >=tq(2010q1)
		replace councilofministers = "$council5"	if g105 >=tq(2010q1)
	}
	else if "$council5" == "qmv1_uw" {
		replace COUNCIL_POS = couofmin_qmv1_uw		if g105 >=tq(2010q1)
		replace councilofministers = "$council5"	if g105 >=tq(2010q1)
	}
	else if "$council5" == "qmv2" {
		replace COUNCIL_POS = couofmin_qmv2			if g105 >=tq(2010q1)
		replace councilofministers = "$council5"	if g105 >=tq(2010q1)
	}
	else if "$council5" == "qmv2_uw" {
		replace COUNCIL_POS = couofmin_qmv2_uw		if g105 >=tq(2010q1)
		replace councilofministers = "$council5"	if g105 >=tq(2010q1)
	}



* ----------------------
* Save temporary file

keep g101-g105 p611-p616 _____GOVERNMENT_POSITION GOV_POS _____COUNCIL_OF_MINISTERS-councilofministers
save tmp_councilofmin, replace

capture: erase zs3.dta
